package lamda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @author LoneRanger
 * @version 1.0
 */
public class StreamAndParallelStreamTest {

	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");

		for (int i = 0; i < 20; i++) {
			System.out.println(list.get(i % list.size()));
		}

	}




	@Test
	public void test0() {
		List<Integer> nums = new ArrayList<>();
		for (int i = 0; i < 1000; i++) {
			nums.add(i);
		}

		//parallelStream 会多线程去处理数据，所以不能使用ArrayList去接收对象，使用CopyOnWriteArrayList 线程安全的集合
		CopyOnWriteArrayList<Integer> nums1 = new CopyOnWriteArrayList<>();
		nums.parallelStream().forEach((num) -> {
			nums1.add(num * 2);
		});

		System.out.println(nums1.size());
	}

	@Test
	public void test1() {
		long l = System.currentTimeMillis();
		List<Integer> nums = new ArrayList<>();
		for (int i = 0; i < 20; i++) {
			nums.add(i);
		}

		nums.parallelStream().forEach((num) -> {
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(num);
		});
		System.out.println(System.currentTimeMillis() - l);
	}


}

/**
 * 长方形
 */
class rectangle {


}



